package com.suse.warmstill.controller;

import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.mapper.TbArticleMapper;
import com.suse.warmstill.service.ArticleBodyService;
import com.suse.warmstill.service.ArticleService;
import com.suse.warmstill.vo.ArticlePostVo;
import com.suse.warmstill.vo.ArticleUVo;
import com.suse.warmstill.vo.ArticleVo;
import com.suse.warmstill.vo.ConditionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 黄靖
 * @author 2022-11-01 18:14
 * description:
 **/
@RestController
@Api(tags = "Pc端 文章管理")
@RequestMapping("pc/articles")
public class PCArticleApi {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleBodyService articleBodyService;

    @Autowired
    private TbArticleMapper articleMapper;



    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("修改文章")
    @PostMapping("/listArticle")
    public CallResult ListArticles(@RequestBody @Validated ArticleUVo article){

        articleService.updateArticle(article )  ;


        return CallResult.success();
    }
    @ApiOperation("根据文章Id 查询文章详情")
    @GetMapping("/detail/{Id}")
    public CallResult<ArticleVo> articldetail(@PathVariable("Id") Long Id) {
        ArticleVo articleVo = articleService.articldetail(Id);

        return CallResult.success(articleVo);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @ApiOperation("发布文章")
    @PostMapping()
    public CallResult publishArticle(@RequestBody @Validated ArticlePostVo article){

        articleService.publishArticle(article);
        return CallResult.success();
    }


    @ApiOperation("删除文章")
    @GetMapping
    @Transactional
    public CallResult delete(Integer id){
        return CallResult.success(articleMapper.deleteById(id));
    }





}
